<?php


namespace app\modules\Merchant\models;


use goodsskupriceModel;
use Illuminate\Database\Eloquent\SoftDeletes;

class dianpuwalletapplyModel extends \Illuminate\Database\Eloquent\Model
{
    protected $table = 'dianpu_wallet_apply';
    protected $primaryKey = 'id';
    protected $appends = ['apply_type_name', 'status_name', 'platform_name', 'apply_info_text'];

    protected static $ALLTYPE = [
        'bank'=>'银行卡转账',
        'wechat'=>'微信零钱',
        'alipay'=>'支付宝'
    ];

    protected static $ALLSTATUS = [
        -1=>'已拒绝',
        0=>'待审核',
        1=>'处理中',
        2=>'已打款',
    ];
    protected static $ALLPLATFORM = [
        'H5'=>'H5',
        'wxOfficialAccount'=>'微信公众号',
        'wxMiniProgram'=>'微信小程序',
        'App'=>'App',
        'merchantBackend'=>'店铺后台',
    ];
    protected function serializeDate($date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function getApplyTypeNameAttribute($value)
    {
        return self::$ALLTYPE[$this->attributes['apply_type']]??'未知';
    }

    public static function getAllType()
    {
        return self::$ALLTYPE;
    }

    public function getStatusNameAttribute($value)
    {
        return self::$ALLSTATUS[$this->attributes['status']]??'未知';
    }

    public static function getAllStatus()
    {
        return self::$ALLSTATUS;
    }

    public function getPlatformNameAttribute($value)
    {
        return self::$ALLPLATFORM[$this->attributes['platform']]??'未知';
    }

    public function getApplyInfoTextAttribute()
    {
        $apply_info_text = '';
        if(!empty($this->attributes['apply_info'])){
            $apply_info_arr = json_decode($this->attributes['apply_info'], true);
            $i = 0;
            if(is_array($apply_info_arr) && count($apply_info_arr)){
                foreach ($apply_info_arr as $key => $value){
                    if($i == 0){
                        $apply_info_text .= $key.':'.$value;
                    }else{
                        $apply_info_text .= PHP_EOL.$key.':'.$value;
                    }
                    $i++;
                }
            }
        }

        return nl2br($apply_info_text);
    }

    public function dianpu()
    {
        return $this->belongsTo(\dianpuModel::class, 'dianpu_id', 'id');
    }

    public function member()
    {
        return $this->belongsTo(\memberModel::class, 'member_id', 'id')->select(['id', 'nickname', 'realname', 'phone', 'avatar']);
    }
}